package co.chuangdian.oflauncher.folders.setting;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import cn.onekit.CONFIG;
import cn.onekit.JSON;
import cn.onekit.LOG;
import cn.onekit.MESSAGE;
import co.chuangdian.oflauncher.R;
import uc.FolderviewType;
import uc.PasswordType;

public class ViewFragment extends Fragment {

	private View view;

	ImageView folder_style_1_icon;
	ImageView folder_style_2_icon;
	FrameLayout folder_style_1;
	FrameLayout folder_style_2;

	JSONObject folder_style;
	int folder_style_int;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.foldersetting_view, container, false);
		return view;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		folder_style_1_icon = (ImageView) view.findViewById(R.id.folder_style_1_icon);
		folder_style_2_icon = (ImageView) view.findViewById(R.id.folder_style_2_icon);
		folder_style_1 = (FrameLayout) view.findViewById(R.id.folder_style_1);
		folder_style_2 = (FrameLayout) view.findViewById(R.id.folder_style_2);

		loadSetting();
		showFolderViewType();

		folder_style_1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				folder_style_2_icon.setVisibility(View.GONE);
				folder_style_1_icon.setVisibility(View.VISIBLE);
				try {
					folder_style = JSON.parse((String) CONFIG.get("folders_setting"));
					folder_style.put("view", 0);
					CONFIG.set("folders_setting", folder_style);
					MESSAGE.send("change_folder_view", null, null);
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		});

		folder_style_2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				folder_style_1_icon.setVisibility(View.GONE);
				folder_style_2_icon.setVisibility(View.VISIBLE);
				try {
					folder_style = JSON.parse((String) CONFIG.get("folders_setting"));
					folder_style.put("view", 1);
					CONFIG.set("folders_setting", folder_style);
					MESSAGE.send("change_folder_view", null, null);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
	}

	void loadSetting() {
		folder_style = JSON.parse((String) CONFIG.get("folders_setting"));
		// folder_style.optInt("view");
		// folder_style_int = folder_style.optInt("view");
	}

	void showFolderViewType() {
		int folder_style_int = folder_style.optInt("view");
		FolderviewType folderview_type = FolderviewType.values()[folder_style_int];
		switch (folderview_type) {
		case normal:
			folder_style_1_icon.setVisibility(View.VISIBLE);
			folder_style_2_icon.setVisibility(View.GONE);
			break;
		case simple:
			folder_style_2_icon.setVisibility(View.VISIBLE);
			folder_style_1_icon.setVisibility(View.GONE);
			break;
		}

	}
}
